package net.cmkj.mine.core.admin.module;

import com.jfinal.aop.Inject;
import net.cmkj.mine.bean.Columns;
import net.cmkj.mine.bean.Modules;
import net.cmkj.mine.bean.SysRes;
import net.cmkj.mine.common.anno.RequiresPermissions;
import net.cmkj.mine.common.controller.BaseController;
import net.cmkj.mine.common.model.InvokeResult;
import net.cmkj.mine.util.CommonUtils;

import java.util.*;

public class InterfaceController extends BaseController {
	
	@Inject
	private InterfaceService service;

    @Inject
    private ModulesService modulesService;
    /**
     * 获取所有字段列表
     * @throws Exception
     */
	@RequiresPermissions(value={"/interface"})
    public void getAllColumns() throws Exception {
        long moduleid = getParaToLong("moduleid");
        renderJson(service.getAllColumns(getPage(), getRows(), moduleid, null, 1, " order by t.ordinal_position"));
    }

    /**
     * 获取返回参数字段列表
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void getParameterColumns() throws Exception {
        long moduleid = getParaToLong("moduleid");
        renderJson(service.getParameterColumns(moduleid, null, 1, " order by t.ordinal_position"));
    }


    /**
     * 获取Header可选择列表
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void getHeaderSearchList() throws Exception {
        long moduleid = getParaToLong("moduleid");
        renderJson(service.getHeaderSearchList(moduleid));
    }

    /**
     * 获取Header已选择列表
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void getHeaderSearchedList() throws Exception {
        long moduleid = getParaToLong("moduleid");
        renderJson(service.getHeaderSearchedList(moduleid));
    }

    /**
     * 获取Header可选择列表
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void getBodySearchList() throws Exception {
        long moduleid = getParaToLong("moduleid");
        renderJson(service.getBodySearchList(moduleid));
    }

    /**
     * 获取Header已选择列表
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void getBodySearchedList() throws Exception {
        long moduleid = getParaToLong("moduleid");
        renderJson(service.getBodySearchedList(moduleid));
    }

    /**
     * 获取Query可选择列表
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void getQuerySearchList() throws Exception {
        long moduleid = getParaToLong("moduleid");
        renderJson(service.getQuerySearchList(moduleid));
    }

    /**
     * 获取Query已选择列表
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void getQuerySearchedList() throws Exception {
        long moduleid = getParaToLong("moduleid");
        renderJson(service.getQuerySearchedList(moduleid));
    }

    /**
     * 修改Header参数
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void updateHeader() throws Exception {
        long moduleid = getParaToLong("moduleid");
        String ids = getPara("ids");
        int type = getParaToInt("type");
        renderJson(service.updateHeader(moduleid,ids,type));
    }

    /**
     * 修改Body参数
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void updateBody() throws Exception {
        long moduleid = getParaToLong("moduleid");
        String ids = getPara("ids");
        int type = getParaToInt("type");
        renderJson(service.updateBody(moduleid,ids,type));
    }

    /**
     * 修改Query参数
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void updateQuery() throws Exception {
        long moduleid = getParaToLong("moduleid");
        String ids = getPara("ids");
        int type = getParaToInt("type");
        renderJson(service.updateQuery(moduleid,ids,type));
    }

    /**
     * 修改请求参数
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void updateColumnsInterfaceName() throws Exception {
        Long id = getParaToLong("id");
        String name = getPara("name");
        renderJson(service.updateColumnsInterfaceName(id,name));
    }

    /**
     * 修改基础参数
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void updateInterface() throws Exception {
        Long id = getParaToLong("id");
        String interfaceUrl = getPara("interfaceUrl");//请求方式
        String successName = getPara("successName");//请求方式
        String successCode = getPara("successCode");//请求方式
        String successMsg = getPara("successMsg");//请求方式
        Integer requestType = getParaToInt("requestType");//请求方式
        Integer isToken = getParaToInt("isToken");//是否Token
        String tokenKey = getPara("tokenKey");//tokenKey
        String tokenValue = getPara("tokenValue");//tokenValue
        Integer tokenPosition = getParaToInt("tokenPosition");//Token位置
        Integer isEncrypt = getParaToInt("isEncrypt");//是否加密
        Integer encryptType = getParaToInt("encryptType");//加密方式
        Integer encryptColumn = getParaToInt("encryptColumn");//加密参数
        renderJson(service.updateInterface(id,interfaceUrl,successName,successCode,successMsg,requestType,isToken,tokenKey,tokenValue,tokenPosition,isEncrypt,encryptType,encryptColumn));
    }


    /**
     * 返回参数添加页面
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void interfaceParameterForm() throws Exception {
        Integer id=this.getParaToInt("id");
        Boolean isadd = this.getParaToBoolean("isadd");
        if(isadd != null && isadd){
            if(id != null){
                SysRes sysResTmp = new SysRes();
                setAttr("res",sysResTmp);
            }
        }else{
            if(id != null){


            }
        }
        render("interfaceParameterForm.html");
    }
















    /**
     * 接口调用
     * @throws Exception
     */
    @RequiresPermissions(value={"/interface"})
    public void sendRequest() throws Exception {
        Long moduleid = getParaToLong("moduleid");
        Modules modules = modulesService.findById(moduleid);
        Map<String, Object> body = new LinkedHashMap<>();
        Map<String, String> header = new HashMap<>();
        Map<String, Object> query = new HashMap<>();
        List<String> fileNames = new ArrayList<>();
        if(modules!=null){
            if(modules.getIsInterface()==2){
                if(CommonUtils.isNotEmpty(modules.getInterfaceUrl())){
                    String headerColumn = modules.getHeaderColumn();
                    String bodyColumn = modules.getBodyColumn();
                    String queryColumn = modules.getQueryColumn();
                    if(CommonUtils.isNotEmpty(headerColumn)){ //header参数
                        String[] headerIds = headerColumn.split(",");
                        for (String id : headerIds) {
                            Columns columns = modulesService.findColumnsById(Long.valueOf(id));
                            if(columns!=null){
                                String name = columns.getInterfaceName();
                                String value = getPara(columns.getColumnName());
                                if(CommonUtils.isNotEmpty(name) && CommonUtils.isNotEmpty(value)){
                                    header.put(name, value);
                                }else{
                                    renderJson(InvokeResult.failure("【"+columns.getColumnComment()+"】数据有误！"));
                                    return;
                                }
                            }
                        }
                    }
                    if(CommonUtils.isNotEmpty(bodyColumn)){ //body参数
                        String[] bodyIds = bodyColumn.split(",");
                        for (String id : bodyIds) {
                            Columns columns = modulesService.findColumnsById(Long.valueOf(id));
                            if(columns!=null){
                                String name = columns.getInterfaceName();
                                String value = "";
                                if(columns.getColumnType().equals("file")){//图片
                                    String fileName = getPara(columns.getColumnName());
                                    if(CommonUtils.isNotEmpty(fileName)){
                                        fileNames.add(fileName);
                                    }
                                }else{
                                    value = getPara(columns.getColumnName());
                                }
                                if(CommonUtils.isNotEmpty(name) && CommonUtils.isNotEmpty(value)){
                                    body.put(name, value);
                                }else{
                                    renderJson(InvokeResult.failure("【"+columns.getColumnComment()+"】数据有误！"));
                                    return;
                                }
                            }
                        }
                    }
                    if(CommonUtils.isNotEmpty(queryColumn)){ //query参数
                        String[] queryIds = queryColumn.split(",");
                        for (String id : queryIds) {
                            Columns columns = modulesService.findColumnsById(Long.valueOf(id));
                            if(columns!=null){
                                String name = columns.getInterfaceName();
                                String value = getPara(columns.getColumnName());
                                if(CommonUtils.isNotEmpty(name) && CommonUtils.isNotEmpty(value)){
                                    query.put(name, value);
                                }else{
                                    renderJson(InvokeResult.failure("【"+columns.getColumnComment()+"】数据有误！"));
                                    return;
                                }
                            }
                        }
                    }
                    renderJson(service.sendRequest(modules,header,body,query,fileNames));
                }else{
                    renderJson(InvokeResult.failure("接口地址有误！"));
                }
            }else{
                renderJson(InvokeResult.failure("未启用接口数据！"));
            }
        }else{
            renderJson(InvokeResult.failure("模块数据有误！"));
        }
    }
}
